package com.leigang.resource.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leigang.common.parent.pojo.dto.ServerConfigDto;
import com.leigang.common.parent.pojo.dto.ServerMonitorDto;
import com.leigang.common.parent.pojo.vo.ServerConfigVo;
import com.leigang.resource.dao.IServerConfigMapper;
import com.leigang.resource.entity.ServerConfig;
import com.leigang.resource.feign.MonitorService;
import com.leigang.resource.service.IServerConfigService;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author leig-a
 * @version 1.0
 * @desc
 * @date 2023/2/16 16:54
 */
@Service
public class ServerConfigServiceImpl extends ServiceImpl<IServerConfigMapper, ServerConfig> implements IServerConfigService {

    private final MonitorService monitorService;

    public ServerConfigServiceImpl(MonitorService monitorService) {
        this.monitorService = monitorService;
    }

    @Override
    public List<ServerConfigVo> listServerConfig(ServerConfigDto serverConfigDto) {
        QueryWrapper<ServerConfig> queryWrapper = Wrappers.query();
        QueryWrapper<ServerConfig> serverConfigQueryWrapper;
        List<ServerConfig> serverConfigList;
        //关键字搜索
        if (StrUtil.isBlank(serverConfigDto.getKeyWord())) {
            serverConfigList = list();
        } else {
            serverConfigQueryWrapper = queryWrapper
                    .like("server_name", serverConfigDto.getKeyWord()).or()
                    .like("server_ip", serverConfigDto.getKeyWord()).or()
                    .like("server_owner", serverConfigDto.getKeyWord()).or()
                    .like("client_monitoring_address", serverConfigDto.getKeyWord());
            serverConfigList = list(serverConfigQueryWrapper);
        }

        serverConfigList = serverConfigList.stream()
                .filter(serverConfig -> Objects.nonNull(serverConfig.getCreateTime()))
                .sorted(
                        Comparator.comparing(ServerConfig::getCreateTime).reversed()
                )
                .collect(Collectors.toList());
        List<ServerConfigVo> serverConfigVoList = new ArrayList<>();
        if (CollUtil.isNotEmpty(serverConfigList)) {
            for (ServerConfig serverConfig : serverConfigList) {
                ServerConfigVo serverConfigVo = new ServerConfigVo();
                BeanUtil.copyProperties(serverConfig, serverConfigVo);
                serverConfigVoList.add(serverConfigVo);
            }
        }
        return serverConfigVoList;
    }


    @Override
    public boolean insertServerConfig(ServerConfigDto serverConfigDto) {
        ServerConfig serverConfig = new ServerConfig();
        BeanUtil.copyProperties(serverConfigDto, serverConfig);
        serverConfig.setId(null);
        serverConfig.setCreateTime(new Date());
        serverConfig.setUpdateTime(new Date());
        boolean flag = save(serverConfig);
        if (flag) {
            monitorService.add(ServerMonitorDto.builder().serverId(serverConfig.getId()).build());
        }
        return true;
    }

    @Override
    public boolean updateServerConfig(ServerConfigDto serverConfigDto) {
        ServerConfig serverConfig = new ServerConfig();
        BeanUtil.copyProperties(serverConfigDto, serverConfig);
        serverConfig.setUpdateTime(new Date());
        return updateById(serverConfig);
    }

    @Override
    public boolean deleteServerConfig(List<String> idList) {
        for (String id : idList) {
            removeById(id);
        }
        return true;
    }
}
